package com.dao.impl;

import static com.def.GenericDef.*;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.util.GUIDGenerator;
import com.dao.IBisBomDao;
import com.model.Bis_bom;
import com.model.Bis_bomId;
import com.tx.Xpbisbom.XpbisbomI;
import com.tx.Xpbisbom.XpbisbomIA;
import com.tx.Xpbisbom.XpbisbomO;
import com.tx.Xpbisbom.XpbisbomOA;
 
@Repository
public class BisBomHibernateDao extends GenericHibernateDao<Bis_bom, Bis_bomId> 
implements IBisBomDao
{
	public XpbisbomO queryByCondition(XpbisbomI xpbisbomI){
		/***
		 * 暂只支持 by mdl_id_fk 查询
		 * **/
		 
		XpbisbomO xpbisbomO = new XpbisbomO();
		xpbisbomO.setTrx_id("XPBISBOM");
		xpbisbomO.setType_id("O");
		List<XpbisbomOA> oaryList = new ArrayList<XpbisbomOA> ();
		
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		String hql ="FROM Bis_bom where 1=1 ";
		String mdl_id_fk = iary.getMdl_id_fk();
		String mtrl_prod_id_fk = iary.getMtrl_prod_id_fk();
		if( mdl_id_fk != null ){
			hql = hql + " and  id.mdl_id_fk='" +  mdl_id_fk  + "'";
		}
		if( mtrl_prod_id_fk != null ){
			hql = hql + " and  id.mtrl_prod_id_fk='" +  mtrl_prod_id_fk  + "'";
		}
		List<Bis_bom> bisBomList =this.find( hql );
		for(Bis_bom bis_bom : bisBomList){
			XpbisbomOA oary = new XpbisbomOA();
			oary.setMdl_id_fk(bis_bom.getId().getMdl_id_fk());
			oary.setMtrl_prod_id_fk(bis_bom.getId().getMtrl_prod_id_fk());
			oary.setMdl_mtrl_seq_id(bis_bom.getId().getMdl_mtrl_seq_id());
			oary.setOpe_id_fk(bis_bom.getOpe_id_fk());
			oary.setMtrl_use_qty(bis_bom.getMtrl_use_qty());
			oary.setMtrl_unit_fk(bis_bom.getMtrl_unit_fk());
			oary.setMnt_flg(bis_bom.getMnt_flg());
			oary.setCns_flg(bis_bom.getCns_flg());
			oaryList.add(oary);
		}
		xpbisbomO.setOary(oaryList);
		xpbisbomO.setTbl_cnt(oaryList.size());
		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg("success");
		return xpbisbomO;
	}
	
//	public XpbisbomO queryQrsKeyList(XpbisbomI xpbisbomI){
//		 
//		XpbisbomO xpbisbomO = new XpbisbomO();
//		xpbisbomO.setTrx_id("XPBISBOM");
//		xpbisbomO.setType_id("O");
//		List<XpbisbomOA> oaryList = new ArrayList<XpbisbomOA> ();
//		
//		XpbisbomIA iary = xpbisbomI.getIary().get(0);
//		String hql ="FROM Bis_bom where 1=1 ";
////		if(iary.getMdl_id_fk()!=null){
////			hql = hql + " and mdl_id_fk='" +  iary.getMdl_id_fk()  + "'";
////		}
////		if(iary.getMdl_typ_fk()!=null){
////			hql = hql + " and mdl_typ_fk='" +  iary.getMdl_typ_fk()  + "'";
////		}
////		if(iary.getOpe_id_fk()!=null){
////			hql = hql + " and ope_id_fk='" +  iary.getOpe_id_fk()  + "'";
////		}
////		if(iary.getOpe_no_fk()!=null){
////			hql = hql + " and ope_no_fk='" +  iary.getOpe_no_fk()  + "'";
////		}
////		if(iary.getOpe_ver_fk()!=null){
////			hql = hql + " and ope_ver_fk='" +  iary.getOpe_ver_fk()  + "'";
////		}
////		if(iary.getPath_id_fk()!=null){
////			hql = hql + " and path_id_fk='" +  iary.getPath_id_fk()  + "'";
////		}
////		if(iary.getPath_ver_fk()!=null){
////			hql = hql + " and path_ver_fk='" +  iary.getPath_ver_fk()  + "'";
////		}
////		if(iary.getTool_id_fk()!=null){
////			hql = hql + " and tool_id_fk='" +  iary.getTool_id_fk()  + "'";
////		}
//		hql = hql + "ORDER BY mdl_typ_fk,mdl_id_fk,path_id_fk,ope_no_fk,ope_id_fk,ope_id_fk,tool_id_fk,qrs_id";
//		List<Bis_bom> bisQrsList =this.find( hql );
//	    String mdl_id_fk="";
//		String mdl_typ_fk="";
//		String ope_id_fk="";
//		String ope_no_fk="";
//		String ope_ver_fk="";
//		String path_id_fk="";
//		String path_ver_fk="";
//		String tool_id_fk="";
//		for(Bis_bom bis_bom : bisQrsList){
//			XpbisbomOA oary = new XpbisbomOA();
////			if( mdl_id_fk.equals( bis_bom.getId().getMdl_id_fk())
////				&& mdl_typ_fk.equals( bis_bom.getId().getMdl_typ_fk())
////				&& ope_id_fk.equals(bis_bom.getId().getOpe_id_fk())
////				&& ope_no_fk.equals( bis_bom.getId().getOpe_no_fk()) 
////				&& ope_ver_fk.equals(bis_bom.getId().getOpe_ver_fk())
////				&& path_id_fk.equals(bis_bom.getId().getPath_id_fk())
////				&& path_ver_fk.equals(bis_bom.getId().getPath_ver_fk())
////				&& tool_id_fk.equals(bis_bom.getId().getTool_id_fk())
////			){
////				continue;
////			}
////			oary.setMdl_id_fk(bis_bom.getId().getMdl_id_fk());
////			oary.setMdl_typ_fk(bis_bom.getId().getMdl_typ_fk());
////			oary.setOpe_id_fk(bis_bom.getId().getOpe_id_fk());
////			oary.setOpe_no_fk(bis_bom.getId().getOpe_no_fk());
////			oary.setOpe_ver_fk(bis_bom.getId().getOpe_ver_fk());
////			oary.setPath_id_fk(bis_bom.getId().getPath_id_fk());
////			oary.setPath_ver_fk(bis_bom.getId().getPath_ver_fk());
////			oary.setTool_id_fk(bis_bom.getId().getTool_id_fk());
////			
////			oary.setQrs_id(bis_bom.getId().getQrs_id());
////			oary.setQrs_ope_id_fk(bis_bom.getQrs_ope_id_fk());
////			oary.setQrs_ope_no_fk(bis_bom.getQrs_ope_no_fk());
////			oary.setQrs_ope_ver_fk(bis_bom.getQrs_ope_ver_fk());
////			oary.setQrs_path_id_fk(bis_bom.getQrs_path_id_fk());
////			oary.setQrs_path_ver_fk(bis_bom.getQrs_path_ver_fk());
////			oary.setQrk_time(bis_bom.getQrk_time());
////			oary.setQrs_time(bis_bom.getQrs_time());
////			oary.setQrs_typ(bis_bom.getQrs_typ());
////			oary.setSwh_path_id_fk(bis_bom.getSwh_path_id_fk());
////			oary.setSwh_path_ver_fk(bis_bom.getSwh_path_ver_fk());
//			oaryList.add(oary);
//			
////			mdl_id_fk   = bis_bom.getId().getMdl_id_fk();
////			mdl_typ_fk  = bis_bom.getId().getMdl_typ_fk();
////			ope_id_fk   = bis_bom.getId().getOpe_id_fk();
////			ope_no_fk   = bis_bom.getId().getOpe_no_fk(); 
////			ope_ver_fk  = bis_bom.getId().getOpe_ver_fk();
////			path_id_fk  = bis_bom.getId().getPath_id_fk();
////			path_ver_fk = bis_bom.getId().getPath_ver_fk();
////			tool_id_fk  = bis_bom.getId().getTool_id_fk() ;
//		}
//		xpbisbomO.setOary(oaryList);
//		xpbisbomO.setTbl_cnt(oaryList.size());
//		xpbisbomO.setRtn_code(RETURN_CODE_OK);
//		xpbisbomO.setRtn_mesg("success");
//		return xpbisbomO;
//	}
	public XpbisbomO addProc(XpbisbomI xpbisbomI){
		XpbisbomO xpbisbomO = new XpbisbomO();
		xpbisbomO.setTrx_id("XPBISBOM");
		xpbisbomO.setType_id("O");
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		Bis_bomId id = new Bis_bomId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		id.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
		id.setMdl_mtrl_seq_id(GUIDGenerator.javaGUID());
		
		Bis_bom queryBisQrs = this.getDataByKeyProc(id);
		if( queryBisQrs != null){
			xpbisbomO.setRtn_code(E_XPBISBOM_RECORDER_ALREADY_EXIST+"");
			xpbisbomO.setRtn_mesg("Record already Exist");
			return xpbisbomO;
		}
		Bis_bom bis_bom = new Bis_bom();
		bis_bom.setId(id);
		bis_bom.setOpe_id_fk(iary.getOpe_id_fk());
		bis_bom.setMtrl_unit_fk(iary.getMtrl_unit_fk());
		bis_bom.setCns_flg(iary.getCns_flg());
		bis_bom.setMnt_flg(iary.getMnt_flg());
		bis_bom.setMtrl_use_qty(iary.getMtrl_use_qty());//TODO 使用数量 如何写入
        this.save(bis_bom);
        
        xpbisbomO.setRtn_code(RETURN_CODE_OK);
        xpbisbomO.setRtn_mesg("success");
		return xpbisbomO;
	}
	public XpbisbomO updateProc(XpbisbomI xpbisbomI){
		XpbisbomO xpbisbomO = new XpbisbomO();
		xpbisbomO.setTrx_id("XPBISBOM");
		xpbisbomO.setType_id("O");
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		Bis_bomId id = new Bis_bomId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		id.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
		id.setMdl_mtrl_seq_id(iary.getMdl_mtrl_seq_id());
//		id.setOpe_id_fk(iary.getOpe_id_fk()); TODO
		Bis_bom bis_bom  = this.getDataByKeyProc(id);
		if(bis_bom ==null){
			xpbisbomO.setRtn_code(E_XPBISBOM_NOT_FOUND_RECORDER+"");
			xpbisbomO.setRtn_mesg("not Found Record[BIS_BOM]");
			return xpbisbomO;
		}
		if(iary.getOpe_id_fk()!=null){
			bis_bom.setOpe_id_fk(iary.getOpe_id_fk());
		}
		if(iary.getMtrl_unit_fk()!=null){
			bis_bom.setMtrl_unit_fk(iary.getMtrl_unit_fk());
		}
		if(iary.getCns_flg()!=null){
			bis_bom.setCns_flg(iary.getCns_flg());
		}
		if(iary.getMnt_flg()!=null){
			bis_bom.setMnt_flg(iary.getMnt_flg());
		}
		if(iary.getMtrl_use_qty()!=null){
			bis_bom.setMtrl_use_qty(iary.getMtrl_use_qty());
		}
		this.save(bis_bom);
		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg("success");
		return xpbisbomO;
	}
	public XpbisbomO deleteProc(XpbisbomI xpbisbomI){
		XpbisbomO xpbisbomO = new XpbisbomO();
		xpbisbomO.setTrx_id("XPBISBOM");
		xpbisbomO.setType_id("O");
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		Bis_bomId id = new Bis_bomId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		id.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
		id.setMdl_mtrl_seq_id(iary.getMdl_mtrl_seq_id());
//		id.setOpe_id_fk(iary.getOpe_id_fk()); 
		Bis_bom bis_bom  = this.getDataByKeyProc(id);
		if( bis_bom == null ){
			xpbisbomO.setRtn_code(E_XPBISBOM_NOT_FOUND_RECORDER+"");
			xpbisbomO.setRtn_mesg("not Found Record[BIS_BOM]");
			return xpbisbomO;
		}
		this.delete(bis_bom);
		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg("success");
		return xpbisbomO;
	}
	public Bis_bom getDataByKeyProc(Bis_bomId id){
		/*****
		 *  String mdl_typ_fk,String mdl_id_fk,String path_id_fk,
			String path_ver_fk,String ope_no_fk ,String ope_id_fk,
	        String ope_ver_fk ,String tool_id_fk,String qrs_id 
		 * *******/
		Bis_bom bisMdlDef = this.get(id);
		return bisMdlDef;
	}
//	public XpbisbomO deleteByCondition(XpbisbomI xpbisbomI){
//		XpbisbomO xpbisbomO = new XpbisbomO();
//		xpbisbomO.setTrx_id("XPBISBOM");
//		xpbisbomO.setType_id("O");
//		XpbisbomIA iary = xpbisbomI.getIary().get(0);
//		String hql ="FROM Bis_bom where  mdl_id_fk='" +  iary.getMdl_id_fk()  + "'";
////			hql = hql + " and mdl_typ_fk='" +  iary.getMdl_typ_fk()  + "'";
////			hql = hql + " and ope_id_fk='" +   iary.getOpe_id_fk()  + "'";
////			hql = hql + " and ope_no_fk='" +   iary.getOpe_no_fk()  + "'";
////			hql = hql + " and ope_ver_fk='" +  iary.getOpe_ver_fk()  + "'";
////			hql = hql + " and path_id_fk='" +  iary.getPath_id_fk()  + "'";
////			hql = hql + " and path_ver_fk='" + iary.getPath_ver_fk()  + "'";
////			hql = hql + " and tool_id_fk='" +  iary.getTool_id_fk()  + "'";
//			
//		List<Bis_bom> bisQrsList =this.find( hql );
//		for(Bis_bom bis_bom : bisQrsList){
//			this.delete(bis_bom);
//		}
//
//		xpbisbomO.setRtn_code(RETURN_CODE_OK);
//		xpbisbomO.setRtn_mesg("success");
//		return xpbisbomO;
//	}
	
}
